/**
  ******************************************************************************
  * @file    cfbsp.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "xc.h"
#include "tskcfg.h"

void Xc_BspInit(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitTypeDef GPIO_HalInitStruct = {0};
  
  /* Peripheral clock enable */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOD);
  
  /* TIM6 interrupt Init */
  NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 3);
  NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
  
  NVIC_SetPriority(TIM1_CC_IRQn, 3);
  NVIC_EnableIRQ(TIM1_CC_IRQn);
  
  NVIC_SetPriority(EXTI0_1_IRQn, 3);
  NVIC_EnableIRQ(EXTI0_1_IRQn);
  
  /* input keys */
  GPIO_HalInitStruct.Pin  = IN07_Pin|IN08_Pin;
  GPIO_HalInitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_HalInitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOD, &GPIO_HalInitStruct);
  
  
  /* TIM1 GPIO Configuration PA8 ---> TIM1_CH1 */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 0xFFFFu;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  TIM_InitStruct.RepetitionCounter = 0;
  LL_TIM_Init(TIM1, &TIM_InitStruct);
	
  LL_TIM_DisableARRPreload(TIM1);
	
  LL_TIM_SetEncoderMode(TIM1, LL_TIM_ENCODERMODE_X4_TI12);
  
  LL_TIM_IC_SetActiveInput(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  LL_TIM_IC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
  
  LL_TIM_IC_SetActiveInput(TIM1, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM1, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM1, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
  LL_TIM_IC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_FALLING);
  
  LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);
  LL_TIM_SetTriggerOutput2(TIM1, LL_TIM_TRGO2_RESET);
  
  LL_TIM_DisableMasterSlaveMode(TIM1);
	
	LL_TIM_DisableIT_UPDATE(TIM1);
	LL_TIM_DisableIT_CC3(TIM1);
  
  TIM1->CR1 |= TIM_CR1_CEN;
}


void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
	if(LL_TIM_IsActiveFlag_UPDATE(TIM1) != 0)
	{
		LL_TIM_ClearFlag_UPDATE(TIM1);
    
	}
}

void TIM1_CC_IRQHandler(void)
{
  if(LL_TIM_IsActiveFlag_CC3(TIM1) != 0)
	{
    XC_TIEROD_STOP();
		
    LL_TIM_ClearFlag_CC3(TIM1);
    
    LL_TIM_DisableIT_CC3(TIM1);
	}
}

void EXTI0_1_IRQHandler(void)
{
  if(__HAL_GPIO_EXTI_GET_FALLING_IT(IN07_Pin) != RESET)
  {
    __HAL_GPIO_EXTI_CLEAR_FALLING_IT(IN07_Pin);
		
		if(Xc_KeyDetect_Shd != NULL)
		{
			xSemaphoreGiveFromISR(Xc_KeyDetect_Shd, NULL);
		}
	}
	
	if(__HAL_GPIO_EXTI_GET_FALLING_IT(IN08_Pin) != RESET)
  {
    __HAL_GPIO_EXTI_CLEAR_FALLING_IT(IN08_Pin);
		
		if(Xc_KeyDetect_Shd != NULL)
		{
			xSemaphoreGiveFromISR(Xc_KeyDetect_Shd, NULL);
		}
	}
}
